package com.gopay.bis.manager.bank.ebank;

import java.util.Calendar;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 平安银行跨行支付对象封装
 * 
 * @KhPayManager.java
 * @author zhengxue
 * @2017-8-14 上午10:18:53 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayManager implements BankManagerRemoteService {
    @Resource(name = "khPayConfig")
    private Map<String, String> khPayConfig;
    
    @Resource(name = "plantBankId")
    private Map<String, String> plantBankId;

    @Resource(name = "khPayBankServiceClient")
    private BankService khPayBankServiceClient;

    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        String masterId = msg.getMerID();
        msg.setBankOrderId(masterId + msg.getOrderId());
        KhPayMessage message = (KhPayMessage) msg;
        message.setMasterId(masterId);
        message.setCurrency(khPayConfig.get("KHPAY.CURRENCY"));
        message.setObjectName("GopayB2C");
        message.setRemark(msg.getBankOrderId());
        message.setPlantId(khPayConfig.get("KHPAY.PLANTID.B2C"));
        message.setPlantBankId(plantBankId.get(message.getBank()));
        BankLogUtils.printB2CManagerSignSendLog(message);
        KhPayMessage retMsg = (KhPayMessage) khPayBankServiceClient.sign(message);
        BankLogUtils.printB2CManagerSignReturnLog(retMsg);
        return retMsg;
    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        KhPayMessage message = (KhPayMessage) msg;
        BankLogUtils.printB2CManagerVerifySendLog(message);
        KhPayMessage retMsg = (KhPayMessage) khPayBankServiceClient.verify(message);
        BankLogUtils.printB2CManagerVerifyReturnLog(retMsg, retMsg.isVerifyResult());
        return retMsg;
    }

    @Override
    public Object query(Object obj) throws GopayException {
        KhPayMessage message = (KhPayMessage) obj;
        message.setTransCode(khPayConfig.get("KHPAY.TRANSCODE.QUERY"));
        message.setMasterId(message.getMerID());
        if (BisBankBusinessProd._12.value.equals(message.getBankBusProd().value)) {
            // 退款查询
            message.setTransCode(khPayConfig.get("KHPAY.TRANSCODE.REFUND.QUERY"));
            
            Calendar c = Calendar.getInstance();
            c.setTime(DateUtils.strToDate(message.getOrderDate()));
            c.add(Calendar.SECOND, -10);
            String beginDate = DateUtils.format(c.getTime());
            message.setBeginDate(beginDate);
            message.setEndDate(DateUtils.getCurrDate());
        }
        BankLogUtils.printB2CManagerQuerySendLog(message, "");
        KhPayMessage retMsg = (KhPayMessage) khPayBankServiceClient.query(message);
        BankLogUtils.printB2CManagerQueryReturnLog(retMsg);
        return retMsg;
    }

    @Override
    public Object refund(BaseMessage base) throws GopayException {
        KhPayMessage message = (KhPayMessage) base;
        message.setTransCode(khPayConfig.get("KHPAY.TRANSCODE.REFUND"));
        message.setMasterId(base.getMerID());
        message.setRefundAmt(base.getAmount().toString());
        message.setCurrency(khPayConfig.get("KHPAY.CURRENCY"));
        message.setObjectName("GopayB2CRefund");
        message.setRemark(message.getBankOrderId());
        message.setNotifyUrl(khPayConfig.get("KHPAY.REFUND.NOTIFYURL"));
        BankLogUtils.printRefundManagerSendLog(message);
        KhPayMessage retMsg = (KhPayMessage) khPayBankServiceClient.refund(message);
        BankLogUtils.printRefundManagerReturnLog(retMsg);
        return retMsg;
    }

    @Override
    public Object dayend(BaseMessage base) throws GopayException, Exception {
        KhPayMessage message = (KhPayMessage) base;
        message.setTransCode(khPayConfig.get("KHPAY.TRANSCODE.QUERY.DETAILS"));
        message.setMasterId(khPayConfig.get("KHPAY.MERID"));
        BankLogUtils.printB2CManagerQuerySendLog(message, "");
        KhPayMessage retMsg = (KhPayMessage) khPayBankServiceClient.dayend(message);
        BankLogUtils.printB2CManagerQueryReturnLog(retMsg);
        return retMsg;
    }

}
